package com.ruicar.afs.cloud.product.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.product.condition.ProductAtomCondition;
import com.ruicar.afs.cloud.product.entity.ProductAtom;
import com.ruicar.afs.cloud.product.enums.ProductTemplateCategoryEnum;
import com.ruicar.afs.cloud.product.enums.StatusEnum;
import com.ruicar.afs.cloud.product.service.ProductAtomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author changyu.gui
 * @version 1.0
 * @date 2020/4/27 17:26
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("产品原子接口")
@RequestMapping("/productAtom")
public class ProductAtomController {
    @Autowired
    private ProductAtomService productAtomService;

    @PostMapping(value = "/queryAtomList")
    @ApiOperation(value = "多条件分页获取产品原子信息数据")
    public IResponse<IPage<ProductAtom>> queryAtomList(@RequestBody QueryCondition<ProductAtomCondition> atomCondition) {
        return IResponse.success(productAtomService.page(new Page(atomCondition.getPageNumber(), atomCondition.getPageSize()), Wrappers.<ProductAtom>query().lambda()
                .like(StringUtils.isNotEmpty(atomCondition.getCondition().getAtomName()), ProductAtom::getAtomName, atomCondition.getCondition().getAtomName())
                .like(StringUtils.isNotEmpty(atomCondition.getCondition().getAtomKey()), ProductAtom::getAtomKey, atomCondition.getCondition().getAtomKey())
                .eq(StringUtils.isNoneEmpty(atomCondition.getCondition().getAtomShowType()), ProductAtom::getAtomShowType, atomCondition.getCondition().getAtomShowType())
                .eq(StringUtils.isNotEmpty(atomCondition.getCondition().getAtomControlType()), ProductAtom::getAtomControlType, atomCondition.getCondition().getAtomControlType())
                .like(StringUtils.isNotEmpty(atomCondition.getCondition().getControlDictionaryType()), ProductAtom::getControlDictionaryType, atomCondition.getCondition().getControlDictionaryType())
                .eq(StringUtils.isNotEmpty(atomCondition.getCondition().getAtomIsDefault()), ProductAtom::getAtomIsDefault, atomCondition.getCondition().getAtomIsDefault())
                .eq(StringUtils.isNotEmpty(atomCondition.getCondition().getAtomTemplateCategory()), ProductAtom::getAtomTemplateCategory, atomCondition.getCondition().getAtomTemplateCategory())
                .eq(StringUtils.isNotEmpty(atomCondition.getCondition().getIsProductModify()), ProductAtom::getIsProductModify, atomCondition.getCondition().getIsProductModify())
        ));
    }

    @RequestMapping(value = "/deleteAtom/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    @Transactional
    @SysLog("删除原子产品信息")
    public IResponse<Boolean> deleteAtom(@PathVariable String id) {
        ProductAtom productAtom = productAtomService.getById(id);
        if (productAtom == null) {
            return IResponse.fail("原子不存在!");
        }
        if (AfsEnumUtil.key(StatusEnum.YES).equals(productAtom.getAtomIsReference())) {
            return IResponse.fail("原子引用过不可删除!");
        }
        if (AfsEnumUtil.key(StatusEnum.YES).equals(productAtom.getAtomIsDefault())) {
            return IResponse.fail("默认原子不可删除!");
        }
        productAtomService.removeById(id);
        return new IResponse<Boolean>().setMsg("通过id删除数据成功");
    }

    @RequestMapping(value = "/addAtom", method = RequestMethod.POST)
    @ApiOperation(value = "新增产品原子")
    @Transactional
    @SysLog("新增产品原子")
    public IResponse<Boolean> addAtom(@RequestBody ProductAtom atom) {
        int count = productAtomService.count(Wrappers.<ProductAtom>query().lambda().eq(ProductAtom::getAtomKey, atom.getAtomKey()));
        if (count > 0) return IResponse.fail("新增产品原子key已存在，不可新增");
          count = productAtomService.count(Wrappers.<ProductAtom>query().lambda().eq(ProductAtom::getAtomName, atom.getAtomName()));
        if (count > 0) return IResponse.fail("新增产品原子名称已存在，不可新增");
        productAtomService.save(atom);
        return new IResponse<Boolean>().setMsg("新增产品原子成功");
    }


    @RequestMapping(value = "/editAtom", method = RequestMethod.POST)
    @ApiOperation(value = "修改产品原子")
    @Transactional
    @SysLog("修改产品原子")
    public IResponse<Boolean> editAtom(@RequestBody ProductAtom atom) {
        ProductAtom productAtom = productAtomService.getById(atom.getId());
        if (AfsEnumUtil.key(StatusEnum.YES).equals(productAtom.getAtomIsReference())) {
            if (!atom.getAtomShowType().equals(productAtom.getAtomShowType()))
                return IResponse.fail("产品原子被使用,原子展示类型不可修改！");
            if (!atom.getAtomControlType().equals(productAtom.getAtomControlType()))
                return IResponse.fail("产品原子被使用,原子控件类型不可修改！");
            if (!atom.getControlDictionaryType().equals(productAtom.getControlDictionaryType()))
                return IResponse.fail("产品原子被使用,控件字典类型不可修改！");
            if (!atom.getAtomTemplateCategory().equals(productAtom.getAtomTemplateCategory()) && !"11".equals(atom.getAtomTemplateCategory()))
                return IResponse.fail("产品原子被使用,原子适用范围只可修改为主产品适用且子产品适用！");
        }
        productAtomService.updateById(atom);
        return new IResponse<Boolean>().setMsg("修改产品原子成功");
    }

    @RequestMapping(value = "/getProductAtomicInformationList", method = RequestMethod.GET)
    @ApiOperation(value = "获取产品原子（分页）")
    public IResponse<IPage<ProductAtom>> getProductAtomicInformation(@ModelAttribute ProductAtomCondition productAtomCondition) {
        if (StrUtil.isBlank(productAtomCondition.getTemplateCategory())) {
            return IResponse.fail("未选择模板类别");
        }
        String[] range = ProductTemplateCategoryEnum.getRange(productAtomCondition.getTemplateCategory());
        if (StrUtil.isNotBlank(productAtomCondition.getAtomTemplateCategory()) && !Arrays.asList(range).contains(productAtomCondition.getAtomTemplateCategory())) {
            return IResponse.fail("原子适用范围选择有误!");
        }
        productAtomCondition.setRange(range);
        Page page = new Page(productAtomCondition.getPageNumber(), productAtomCondition.getPageSize());
        IPage<List<ProductAtom>> list = productAtomService.getProductTemplateDetails(page, productAtomCondition);
        return IResponse.success(list);
    }

    @RequestMapping(value = "/getProducttemplateAtomList", method = RequestMethod.POST)
    @ApiOperation(value = "获取产品原子（分页）")
    public IResponse<IPage<ProductAtom>> getProducttemplateAtomList(@RequestBody QueryCondition<ProductAtomCondition> productAtomCondition) {
        String templateCategory = productAtomCondition.getCondition().getTemplateCategory();
        if (StrUtil.isBlank(templateCategory)) {
            return IResponse.fail("未选择模板类别");
        }
        Page page = new Page(productAtomCondition.getPageNumber(), productAtomCondition.getPageSize());
        IPage<List<ProductAtom>> list = productAtomService.getProductTemplateDetails(page, productAtomCondition.getCondition());
        return IResponse.success(list);
    }

}

